<template>
  <div class="staffReportStatistics">
    <DFPageList ref="dfPageListRef" :page-options="pageOptions" />
  </div>
</template>

<script lang="ts" setup name="StaffReportStatistics">
  import { ref } from 'vue';
  import lang from './lang';
  import { useMixins } from '@/hooks/useMixins';
  import { getDateStr, disablePreviousDate2 } from '@/utils/dateUtil';
  import { listUser } from '@/api/system/user';
  import {
    pageStaffReportStatistics,
    exportStaffReportStatistics,
  } from '@render/api/reportForm/staffReportStatistics';

  const { loadLangFn, t } = useMixins();

  const dfPageListRef: any = ref(null);

  loadLangFn(lang);

  const reportDate = ref({
    createEndTime: `${getDateStr(0)} 23:59:59`,
    createStartTime: `${getDateStr(-6)} 00:00:00`,
    defaultValue: [
      new Date(`${getDateStr(-6)} 00:00:00`).getTime(),
      new Date(`${getDateStr(0)} 23:59:59`).getTime(),
    ],
  });

  const setRequestDataFn = (e) => {
    const { entity } = e;
    entity.reportDateStartTime = reportDate.value.createStartTime;
    entity.reportDateEndTime = reportDate.value.createEndTime;
    // entity.exceptionFlag = 0; //异常标识 0无异常，1有异常
    return {
      ...e,
      entity,
    };
  };

  const beforeResetFn = () => {
    reportDate.value.createStartTime = `${getDateStr(-6)} 00:00:00`;
    reportDate.value.createEndTime = `${getDateStr(0)} 23:59:59`;
  };

  // const shiftList = ref([]);

  const userList = ref([]);

  const pageOptions = {
    searchForm: {
      formSchemas: [
        {
          label: 'nickName',
          prop: 'userIdList',
          common: true,
          component: 'NSelect',
          options: userList,
          componentProps: {
            multiple: true,
          },
        },
        {
          label: 'reportDate',
          component: 'NDatePicker',
          defaultValue: reportDate.value.defaultValue,
          componentProps: {
            clearable: false,
            type: 'daterange',
            valueFormat: 'yyyy-MM-dd',
            format: 'yyyy-MM-dd',
            defaultTime: ['00:00:00', '23:59:59'],
            isDateDisabled: disablePreviousDate2,
            onUpdateValue: (_, v: string[]) => {
              if (v) {
                reportDate.value.createStartTime = v[0];
                reportDate.value.createEndTime = v[1];
              } else {
                reportDate.value.createStartTime = '';
                reportDate.value.createEndTime = '';
              }
            },
          },
        },
        {
          label: 'fastSearch',
          component: 'NInput',
          componentProps: {
            placeholder: t('staffReportStatistics.table.plInput'),
          },
        },
      ],
      beforeResetFn,
    },
    table: {
      hasSelection: true,
      hasCreate: false,
      columns: [
        {
          label: 'userName',
          prop: 'userNameList',
          common: true,
        },
        {
          label: 'nickName',
          prop: 'nickNameList',
          common: true,
        },
        {
          label: 'materialCode',
        },
        {
          label: 'materialName',
        },
        {
          label: 'version',
        },
        {
          label: 'materialStandards',
        },
        {
          label: 'reportDate',
          prop: 'reportTime',
        },
        {
          label: 'reportNum',
        },
        {
          label: 'goodNum',
        },
        {
          label: 'badNum',
        },
        {
          label: 'yieldRate',
        },
      ],
      operaColumn: [
        {
          label: 'export',
        },
      ],
      requestFn: pageStaffReportStatistics,
      setRequestDataFn,
      exportRequestFn: exportStaffReportStatistics,
    },
  };

  const initDictData = async () => {
    const res = await Promise.all([listUser()]);

    const [res2] = res;

    userList.value = res2.data.map((item) => ({
      label: `${item.nickName}(${item.userName})`,
      value: item.id,
    }));
  };

  initDictData();
</script>
